/**
 * 登录注册入口文件
 */
define(function (require, exports, module) {


    var $welcome = $(".welcome"), $form = $welcome.find("form")
    var toolTip = require("ui/tip"),
        UI = require("js/UI");


    var reg_phone = /^[1][358][0-9]{9}$/;
    var reg_password = /^[a-zA-Z0-9]{6,16}$/;
    var password_rule = "密码为6-12个数字或字母组合";

    //<editor-folder desc='协议选择'>
    var $dealSpan = $form.find(".txt span");
    $dealSpan.unbind("click.selected").bind("click.selected", function () {
        var $this = $(this), $ipt = $this.siblings("input[type='hidden']");
        if ($this.hasClass("selected")) {
            $ipt.val(false)
            $this.removeClass("selected")
        } else {
            $ipt.val(true)
            $this.addClass("selected");
        }
    })
    //</editor-folder>

    var focusFun = function ($this) {
        setTimeout(function () {
            $this[0].select();
        }, 0)
    }


    var $GIpt = $form.find(".G_ipt").each(function () {
        var $this = $(this), _placeholder = $this.attr("placeholder");
        var $par = $this.parent(), dir = "right";
        if ($par.hasClass("verify") || $par.hasClass("note")) {
            dir = "left"
        }


        var tipInst = new toolTip($this, _placeholder, dir)
        $this.unbind("focus.ipt").bind("focus.ipt", function (event, msg) {
            $this.attr("placeholder", "")
            var _tipMsg = msg ? msg : _placeholder
            if ($par.hasClass("password")) {
                _tipMsg = password_rule;
            }
            tipInst.show(_tipMsg, false);


        }).unbind("blur.ipt").bind("blur.ipt", function () {
            $this.attr("placeholder", _placeholder);
            var _val = $.trim($this.val());
            if (_val) {
                if ($par.hasClass("phone")) {
                    if (!_val.match(reg_phone)) {
                        tipInst.show("请按照正确手机号码格式输入");
                    }
                }
                if ($par.hasClass("password")) {
                    if (!_val.match(reg_password)) {
                        tipInst.show(password_rule);
                        focusFun($this)
                    } else {
                        if ($this.hasClass("pwd")) {
                            var _pwd = $.trim($GIpt.filter(".password").eq(0).val())
                            if (_pwd != _val) {
                                tipInst.show("密码不一致 请重新输入");
                                focusFun($this)
                            }
                        }
                    }
                }
            }

        })
    })

    //提交
    $form.find("[type='submit']").unbind("click.sub").bind("click.sub", function () {
        var validate_success = true;
        $GIpt.each(function () {
            var $this = $(this), _val = $this.val(), $par = $this.parent(), _placeholder = $this.attr("placeholder");
            var dir = "right";
            if ($par.hasClass("verify") || $par.hasClass("note")) {
                dir = "left"
            }
            var tipInst = new toolTip($this, _placeholder, dir)
            if (!_val) {
                var _msg = "请输入用户名"
                if ($par.hasClass("password")) {
                    _msg = "请输入密码"
                } else if ($par.hasClass("phone")) {
                    _msg = "请输入手机号码"
                } else if ($par.hasClass("note")) {
                    _msg = "请输入验证码"
                }
                $this.trigger("focus.ipt", [_msg])
                validate_success = false;
                return false
            } else {
                if ($par.hasClass("phone")) {
                    if (!_val.match(reg_phone)) {
                        tipInst.show("请按照正确手机号码格式输入");
                        validate_success = false;
                    }
                }
                if ($par.hasClass("password")) {
                    if (!_val.match(reg_password)) {
                        tipInst.show(password_rule);
                        focusFun($this)
                        validate_success = false;
                    } else {
                        if ($this.hasClass("pwd")) {
                            var _pwd = $.trim($GIpt.filter(".password").eq(0).val())
                            if (_pwd != _val) {
                                tipInst.show("密码不一致 请重新输入");
                                focusFun($this)
                                validate_success = false;
                            }
                        }
                    }
                }
            }
        })

        var _deal = $dealSpan.siblings("input").val()
        if (_deal == "false") {
            var tipInst = toolTip($dealSpan, "请选择同意协议", "left");
            tipInst.show("请选择同意协议");
            validate_success = false;
            return false
        }


        if (!validate_success) {
            return false
        }
    })
})